/*
 * Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.com.customer.manager.adapter;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xuexiang.xui.adapter.recyclerview.BaseRecyclerAdapter;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.adapter.recyclerview.sticky.FullSpanUtils;
import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.xuexiang.xui.widget.imageview.strategy.impl.GlideImageLoadStrategy;

import cn.com.customer.manager.Constants;
import cn.com.customer.manager.R;
import cn.com.customer.manager.bean.StickyItem;


/**
 * @author wjy
 * @since 2020/4/25 1:23 AM
 */
public class CustomerAdapter extends BaseRecyclerAdapter<StickyItem> {

    /**
     * 头部标题
     */
    public static final int TYPE_HEAD_STICKY = 1;
    /**
     * 客户
     */
    private static final int TYPE_NEW_INFO = 2;

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        FullSpanUtils.onAttachedToRecyclerView(recyclerView, this, TYPE_HEAD_STICKY);
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerViewHolder holder) {
        FullSpanUtils.onViewAttachedToWindow(holder, this, TYPE_HEAD_STICKY);
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position).isHeadSticky()) {
            return TYPE_HEAD_STICKY;
        } else {
            return TYPE_NEW_INFO;
        }
    }

    @Override
    public int getItemLayoutId(int viewType) {
        if (viewType == TYPE_HEAD_STICKY) {
            return R.layout.adapter_vlayout_title_item;
        } else {
            return R.layout.item_customer_rv;
        }
    }

    @Override
    protected void bindData(@NonNull RecyclerViewHolder holder, int position, StickyItem item) {
        if (item == null) {
            return;
        }

        if (item.isHeadSticky()) {
            holder.text(R.id.tv_title, item.getHeadTitle());
        } else {
            RadiusImageView imageView = (RadiusImageView) holder.getView(R.id.iv_photo);
            TextView tags = (TextView) holder.getView(R.id.tv_tags);
//            ImageLoader.get().loadImage(imageView, Constants.BASE_URL + item.getHeadTitle());
            new GlideImageLoadStrategy().loadImage(imageView,Constants.BASE_IMAGE_URL + item.getCustomerInfo().getHeadUrl());
            holder.text(R.id.tv_name, item.getCustomerInfo().getName());
            holder.text(R.id.tv_role, "职位："+item.getCustomerInfo().getDuty());
            holder.text(R.id.tv_phone, "电话："+item.getCustomerInfo().getMobile());
            holder.text(R.id.tv_message, "");//邮箱
            if(item.getCustomerInfo().getTags().size()==0){
                tags.setVisibility(View.GONE);
            }else {
                tags.setVisibility(View.VISIBLE);
                tags.setText(item.getCustomerInfo().getTags().get(0).getTagName());
            }

        }
    }
}
